package com.pxst.fi.controller;

import com.pxst.exception.BusinessException;
import com.pxst.fi.entity.FiExcelModel;
import com.pxst.fi.entity.ShTrxExcelModel;
import com.pxst.fi.req.MerchantStatementReq;
import com.pxst.fi.resp.MerchantStatementResp;
import com.pxst.fi.service.MerchantStatementService;
import com.pxst.resp.ApiResp;
import com.pxst.resp.PageResp;
import com.pxst.utils.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.ValidationException;
import java.io.IOException;
import java.util.List;

/**
 * @author rj
 * @create 2023-09-19-11:15
 */
@Api(tags = "商户对账单")
@RestController
@RequestMapping("/shTrx")
@Validated
public class MerchantStatementController {

    @Autowired
    private MerchantStatementService merchantStatementService;

    @GetMapping
    @ApiOperation(value = "商户对账单查询")
    public ApiResp<PageResp<MerchantStatementResp>> getAll(MerchantStatementReq merchantStatementReq) {
        if (ObjectUtils.isEmpty(merchantStatementReq.getMerchantId()) && StringUtils.isEmpty(merchantStatementReq.getTransactionId())) {
            throw new ValidationException("请选择商户或输入Id");
        }
        return merchantStatementService.getAllByCondition(merchantStatementReq);
    }

    @GetMapping("/download")
    @ApiOperation("汇出报表")
    public void download(MerchantStatementReq merchantStatementReq, HttpServletResponse response){
        List<ShTrxExcelModel> excelModels = merchantStatementService.download(merchantStatementReq);
        try {
            ExcelUtils.exportExcel(excelModels, "商户对账单汇出报表", "商户对账单报表", ShTrxExcelModel.class, "CashFlow-", response);
        } catch (IOException e) {
            e.printStackTrace();
            throw new BusinessException("汇出报表错误");
        }
    }
}
